package controllers

import (
	sysv1 "gmap/api/v1"

	v1 "k8s.io/api/core/v1"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/event"
)

// var gray_annotation = "sys.k8s.99bill.com/gray"

type ResourceAnnotationPredicate struct {
}

func (r *ResourceAnnotationPredicate) Create(e event.CreateEvent) bool {
	return predict(e.Object)
}

func (r *ResourceAnnotationPredicate) Update(e event.UpdateEvent) bool {
	if _, ok := e.ObjectNew.(*v1.Namespace); ok {
		return false
	}
	return predict(e.ObjectNew)
}

func (r *ResourceAnnotationPredicate) Delete(e event.DeleteEvent) bool {
	if _, ok := e.Object.(*v1.Namespace); ok {
		return false
	}
	return predict(e.Object)
}

func (r *ResourceAnnotationPredicate) Generic(e event.GenericEvent) bool {
	return predict(e.Object)
}

func predict(obj client.Object) bool {
	_, ok := obj.(*sysv1.Gmap)
	if ok {
		return true
	}
	_, ok = obj.(*v1.Namespace)
	if ok {
		return ContainsAnnotation(obj, GRAY_ANNOTATION)
	}

	// if _, ok := obj.(*v1.ConfigMap); ok {
	// 	return containsAnnotation(obj, gray_annotation)
	// }

	return false
}
